package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.interfaces.AdapterInterface;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.response.SearchResultResponse;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.DensityUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.widgets.FlowLayout;
import cn.wit.summit.chwan.widgets.ItemListView;

/**
 * Created by Alan on 2018/4/4.
 */

public class SearchResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private AdapterInterface mAdapterInterface;
    private int viewpageIndex = 0;
    private int textWidth,  height, marginLeft, marginTop;
    private LinearLayout.LayoutParams paramsImage;
    private RelativeLayout.LayoutParams paramsImageRl;

    private Map<String, String> downTemp = new HashMap<>();

    public SearchResultAdapter(Activity context, List<ViewBean> datas, AdapterInterface mAdapterInterface) {
        this.mContext = context;
        this.mDatas = datas;
        this.mAdapterInterface = mAdapterInterface;
        inflater = LayoutInflater.from(mContext);

        textWidth = DensityUtil.dp2px(mContext, 65);
        height = DensityUtil.dp2px(mContext, 24);
        marginLeft = DensityUtil.dp2px(mContext, 10);
        marginTop = DensityUtil.dp2px(mContext, 10);

        Display display = ((MyActivity)context).getWindowManager().getDefaultDisplay();
        int w = display.getWidth();
        int h = w * 302 / 720;
        paramsImage = new LinearLayout.LayoutParams(w, h);
        paramsImageRl = new RelativeLayout.LayoutParams(w, h);
    }

    public void setViewpageIndex(int viewpageIndex) {
        this.viewpageIndex = viewpageIndex;
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size()>0 && downTemp.containsKey(gameId)){
            result = downTemp.get(gameId);
        }
        return result;
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == SearchResultAdapter.ViewType.TYPE_LIST) {
            GameListBaseBean list = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return list;
        }
        return null;
    }

    public enum ViewType {
        TYPE_NULL,
        TYPE_TAB,
        TYPE_IMAGE,
        TYPE_AD,
        TYPE_LINE_SMALL,
        TYPE_LIST
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_TAB.ordinal()) {
            holder = new TabHolder(inflater.inflate(R.layout.item_search_tab_view, parent, false));
        } else if (viewType == ViewType.TYPE_IMAGE.ordinal()) {
            holder = new ImageHolder(inflater.inflate(R.layout.item_search_image_view, parent, false));
        } else if (viewType == ViewType.TYPE_AD.ordinal()) {
            holder = new AdHolder(inflater.inflate(R.layout.item_search_ad_view, parent, false));
        } else if (viewType == ViewType.TYPE_LIST.ordinal()) {
            holder = new ListHolder(inflater.inflate(R.layout.item_search_list_view, parent, false));
        } else if (viewType == ViewType.TYPE_NULL.ordinal()) {
            holder = new NullHolder(inflater.inflate(R.layout.empty_search_view, parent, false));
        } else if (viewType == ViewType.TYPE_LINE_SMALL.ordinal()) {
            holder = new LineSmallHolder(inflater.inflate(R.layout.item_daily_new_small_line_view, parent, false));
        }

        return holder;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof TabHolder) {
            TabHolder tabHolder = (TabHolder) holder;
            tabHolder.itemSearchFl.removeAllViews();
            final List<SearchResultResponse.TagInfoBean> tempList = (List<SearchResultResponse.TagInfoBean>) viewBean.getObj();

            for (int i = 0; i < tempList.size(); i++) {
                final TextView mTextView = new TextView(mContext);
                mTextView.setText(tempList.get(i).getName()+"("+tempList.get(i).getCount()+")");
                mTextView.setSingleLine();
                if (tempList.get(i).getCount()>9999)
                    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                else
                    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                mTextView.setGravity(Gravity.CENTER);
                mTextView.setBackgroundResource(R.drawable.tab_bg);
                if (i == viewpageIndex) {
                    mTextView.setSelected(true);
                    mTextView.setTextColor(Color.parseColor("#5791f7"));
                } else {
                    mTextView.setSelected(false);
                    mTextView.setTextColor(Color.parseColor("#5D5D5D"));
                }
                mTextView.setTag(i);
                mTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int num = Integer.parseInt(mTextView.getTag().toString());
                        if (tempList.get(num).getCount()>0) {
                            viewpageIndex = num;
//                        notifyItemChanged(position);
                            mAdapterInterface.onAdapterItemClick(v, tempList.get(viewpageIndex).getId());
                        }
                    }
                });
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(textWidth, height);
                params.setMargins(marginLeft, marginTop, 0, 0);
                mTextView.setLayoutParams(params);
                tabHolder.itemSearchFl.addView(mTextView);
            }
        } else if (holder instanceof ImageHolder) {
            SearchResultResponse.AdInfoBean temp = (SearchResultResponse.AdInfoBean) viewBean.getObj();
            ImageHolder imageHolder = (ImageHolder) holder;
            imageHolder.itemSearchIv.setLayoutParams(paramsImage);
            imageHolder.itemSearchIv.setImageURI(temp.getJump_cover_big_pic());
            imageHolder.itemSearchIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAdapterInterface.onAdapterItemClick(v, position);
                }
            });
        } else if (holder instanceof AdHolder) {
            final JumpBaseBean temp = (JumpBaseBean) viewBean.getObj();
            AdHolder adHolder = (AdHolder) holder;
            adHolder.xiyuanBt.setText(temp.getJump_title()+" >");
            adHolder.xiyuanTx.setText(temp.getJump_sub_title());
            adHolder.itemSearchxyIv.setLayoutParams(paramsImageRl);
            adHolder.itemSearchxyIv.setImageURI(temp.getJump_cover_big_pic());
            adHolder.itemSearchRootRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (AccountUtil.getInstance().getUidNotToLogin(mContext)==0)
                        return;
                    IntentUtil.getInstance().intentTo(mContext, temp);
//                    mAdapterInterface.onAdapterItemClick(v, position);
                }
            });
        } else if (holder instanceof ListHolder) {
            GameListBaseBean listTemp = (GameListBaseBean) viewBean.getObj();
            downTemp.put(listTemp.getGame_id(), position+"");
            ListHolder listHolder = (ListHolder) holder;
            listHolder.listItem.setData(mContext,listTemp);
            listHolder.listItem.hitLine();
        } else if (holder instanceof NullHolder) {
            NullHolder nullHolder = (NullHolder) holder;
            nullHolder.emptyRootLl.setVisibility(View.VISIBLE);
        }
    }

    class NullHolder extends RecyclerView.ViewHolder {
        LinearLayout emptyRootLl;

        public NullHolder(View view) {
            super(view);
            emptyRootLl = view.findViewById(R.id.emptyRootLl);
        }
    }

    class TabHolder extends RecyclerView.ViewHolder {
        FlowLayout itemSearchFl;

        public TabHolder(View view) {
            super(view);
            itemSearchFl = view.findViewById(R.id.itemSearchFl);
        }
    }

    class ImageHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView itemSearchIv;

        public ImageHolder(View view) {
            super(view);
            itemSearchIv = view.findViewById(R.id.itemSearchIv);
        }
    }

    class AdHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView itemSearchxyIv;
        TextView xiyuanBt;
        TextView xiyuanTx;
        RelativeLayout itemSearchRootRl;

        public AdHolder(View view) {
            super(view);
            itemSearchxyIv = view.findViewById(R.id.itemSearchxyIv);
            xiyuanBt = view.findViewById(R.id.xiyuanBt);
            xiyuanTx = view.findViewById(R.id.xiyuanTx);
            itemSearchRootRl = view.findViewById(R.id.itemSearchRootRl);
        }
    }

    class ListHolder extends RecyclerView.ViewHolder {
        ItemListView listItem;

        public ListHolder(View view) {
            super(view);
            listItem = view.findViewById(R.id.listItem);
        }
    }

    class LineSmallHolder extends RecyclerView.ViewHolder {

        public LineSmallHolder(View view) {
            super(view);
        }
    }
}
